package com.xjlsoft.weather.util;


import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;


/**
 * 
 * Timestamp格式的时间解析器
 * 
 * 到导出的 数据格式时间 为 Timestamp时  需要  转为器
 * */
public class TimestampConverter implements Converter<Timestamp> {

	@Override
	public Class<Timestamp> supportJavaTypeKey() {
		return Timestamp.class;
	}

	@Override
	public CellDataTypeEnum supportExcelTypeKey() {
		return CellDataTypeEnum.STRING;
	}

	@Override
	public Timestamp convertToJavaData(@SuppressWarnings("rawtypes") CellData cellData, ExcelContentProperty contentProperty,
	                                       GlobalConfiguration globalConfiguration) {
		String value=cellData.getStringValue();
		//return LocalDateTime.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    Date date;
		try {
			date = fmt.parse(value);
			Timestamp newTimestamp=new Timestamp(date.getTime());
			return newTimestamp;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;

	}

	@Override
	public CellData<String> convertToExcelData(Timestamp value, ExcelContentProperty contentProperty,
	                                           GlobalConfiguration globalConfiguration) {
		//return new CellData<>(value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
		Date start=new Date(value.getTime());
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String value1=formatter.format(start);
		return new CellData<>(value1);
	}
	

}
